package com.example.dzbsh.controller;

import com.example.dzbsh.service.JiashuService;
import com.example.dzbsh.service.RenyuanService;
import com.example.dzbsh.service.ZhenbieQingkuangService;
import com.example.dzbsh.util.CreateWord;
import com.example.dzbsh.util.JsonResult;
import com.example.dzbsh.util.PageInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("zhenbieqingkuang")
public class ZhenbieQingkuangController {

    @Value("${userfilepath}")
    private String userfilepath;

    @Resource
    ZhenbieQingkuangService ddService;
    @Resource
    RenyuanService ddRenyuanService;
    @Resource
    JiashuService ddJiashuService;

    // 获得不同状态的甄别数据的数量
    @RequestMapping("findzhenbienum")
    public JsonResult<?> findZhenbieNum(){
        try {
            int mmAllNum = ddService.countByShifouGuifan("");//所有
            int mmWeipanduanNum = ddService.countByShifouGuifan(null);//未判断
            int mmXuguifanNum = ddService.countByShifouGuifan("1");//需要规范
            int mmBuxuguifanNum = ddService.countByShifouGuifan("0");//不需要规范

            Map<String, Object> mmReturnMap = new HashMap<String, Object>();
            mmReturnMap.put("allnum", mmAllNum);
            mmReturnMap.put("weipanduannum", mmWeipanduanNum);
            mmReturnMap.put("xuguifannum", mmXuguifanNum);
            mmReturnMap.put("buxuguifannum", mmBuxuguifanNum);
            return JsonResult.getSuccessResult(mmReturnMap);
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.getErrorResult("zhenbieqingkuang/findzhenbienum:error " + e.getMessage());
        }
    }

    @RequestMapping("find_by_jiashuid")
    public JsonResult<?> findByJiashuid(@RequestParam("jiashuid") String ppJiashuId) {
        try {
            return  JsonResult.getSuccessResult(ddService.findByJiashuId(ppJiashuId));
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.getErrorResult("chaheqingkuang/findByJiashuid:error " + e.getMessage());
        }
    }

    @RequestMapping("find_allrenyuan")
    public JsonResult<?> findAllRenyuan(@RequestParam("deptid")String ppDeptId, @RequestParam("truename")String ppTrueName, @RequestParam("startindex") int ppPageIndex,
                                        @RequestParam("pagesize") int ppPageSize) {
        try {
            List<Map<String, Object>> mmList=ddService.findAllRenyuan(ppDeptId,ppTrueName,ppPageIndex,ppPageSize);
            Integer mmCount = ddService.getCustomCountByvalid(ppDeptId,ppTrueName);
            PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);

            Map<String, Object> mmMap = new HashMap<String, Object>();
            mmMap.put("RenyuanList", mmList);
            mmMap.put("PageInfo", mmPageInfo);
            return JsonResult.getSuccessResult(mmMap);
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.getErrorResult("ZhenbieQingkuangController/findAllRenyuan:error " + e.getMessage());
        }
    }

    @RequestMapping("find_alljiashu_by_renyuan")
    public JsonResult<?> findAllJiashuByRenyuan(@RequestParam("renyuanid")String ppRenyuanId) {
        try {

            return JsonResult.getSuccessResult(ddService.findAllJiashuByRenyuan(ppRenyuanId));
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.getErrorResult("ZhenbieQingkuangController/findAllJiashuByRenyuan:error " + e.getMessage());
        }
    }

    @RequestMapping("find_zhenbiexiangqing_by_renyuanid")
    public JsonResult<?> findZhenbieXiangqingByRenyuanId(@RequestParam("renyuanid")String ppRenyuanId) {
        try {

            return JsonResult.getSuccessResult(ddService.findZhenbieXiangqingByRenyuanId(ppRenyuanId));
        }catch (Exception e) {
            e.printStackTrace();
            return JsonResult.getErrorResult("ZhenbieQingkuangController/findZhenbieXiangqingByRenyuanId:error " + e.getMessage());
        }
    }

    @RequestMapping("daochu")
    public void daoChu(@RequestParam("renyuanid") String ppRenyuanId, HttpServletRequest request, HttpServletResponse response){
        try {
            Map<String,Object> mmReturnMap = ddService.Daochurending(ppRenyuanId);
            if (userfilepath.endsWith("/")) {
                userfilepath = userfilepath.substring(0, userfilepath.length() - 1);
            }
            SimpleDateFormat mmDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String mmSavePath = userfilepath + "/temp/zhenbieqingkuang/"+mmDateFormat.format(new Date())+"/";
            File mmFold = new File(mmSavePath);
            if (!mmFold.isDirectory() || !mmFold.exists()) {
                mmFold.mkdirs();
            }

            CreateWord.createWord(userfilepath, mmSavePath,  ppRenyuanId+"xingweirendingbiao.doc", mmReturnMap, request,
                    response,"rd","1");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    // 甄别汇总表查询使用
    @RequestMapping("find_custom_by_deptid")
    public JsonResult<?> findCustomByDeptId(@RequestParam("deptid") String ppDeptId,@RequestParam("renyuanleibie") String ppRenyuanleibieId,
                                            @RequestParam("jigoumingchengma") String ppJigoumingchengma, @RequestParam("paixu") String ppPaixu,
                                            @RequestParam("pageindex") int ppPageIndex, @RequestParam("pagesize") int ppPageSize){
        try {
            List<Map<String,Object>> mmList = ddRenyuanService.findCustomRenyuanByDeptId(ppDeptId,ppRenyuanleibieId,ppJigoumingchengma,ppPaixu,ppPageIndex,ppPageSize); // 查询该单位的干部信息列表
            List<Map<String,Object>> mmReturnList = new ArrayList<>();
            for (int i = 0; i < mmList.size(); i++) {
                int mmRenyuanHebingshu = 0;
                Map<String,Object> mmMap = mmList.get(i);
                if (mmMap!=null){
                    List<Map<String,Object>> mmReturnJiashuList = new ArrayList<>();

                    String mmRenyuanId = mmMap.get("renyuanid") == null ? "" : mmMap.get("renyuanid").toString();

                    List<Map<String,Object>> mmJiashuList = ddJiashuService.findByRenyuanid(mmRenyuanId); // 查询该领导的家属集合
                    for (int j = 0; j < mmJiashuList.size(); j++) {
                        int mmjiashuhebing = 0;
                        Map<String,Object> mmJiashuMap = mmJiashuList.get(j);

                        if (mmJiashuMap!=null){

                            String mmJiashuId = mmJiashuMap.get("jiashuid") == null ? "" : mmJiashuMap.get("jiashuid").toString();

                            List<Map<String,Object>> mmZhenbieqingkuangList = ddService.findByJiashuId(mmJiashuId); // 查询该家属的甄别情况
                            String mmIsZhenbie = "否";
                            if (mmZhenbieqingkuangList.size()>0){
                                mmIsZhenbie = "是";
                                mmjiashuhebing = mmZhenbieqingkuangList.size();
                                mmRenyuanHebingshu = mmRenyuanHebingshu+mmjiashuhebing;
                            }else{
                                mmRenyuanHebingshu = mmRenyuanHebingshu+1;
                            }

                            mmJiashuMap.put("zhenbielist",mmZhenbieqingkuangList);
                            mmJiashuMap.put("shifoujingshang",mmIsZhenbie);
                            mmJiashuMap.put("hebinghang",mmjiashuhebing);
                            mmReturnJiashuList.add(mmJiashuMap);
                        }


                    }
                    mmMap.put("hebinghang",mmRenyuanHebingshu);
                    mmMap.put("jiashuzhenbielist",mmReturnJiashuList);
                }
                mmReturnList.add(mmMap);
            }
            Integer mmCount = ddRenyuanService.getCustomCountByGuanliDeptId(ppDeptId,ppRenyuanleibieId,ppJigoumingchengma);

            PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);

            Map<String, Object> mmReturnMap = new HashMap<String, Object>();
            mmReturnMap.put("RenyuanList", mmReturnList);
            mmReturnMap.put("PageInfo", mmPageInfo);
            return JsonResult.getSuccessResult(mmReturnMap);
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.getErrorResult("zhenbieqingkuang/find_custom_by_deptid:error " + e.getMessage());
        }
    }
}
